package com.games.fifteenpieces;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
	
	
public class GameActivity extends Activity {
	public static final int DIALOG_GAME_OVER_ID = 0, DIALOG_CLOSE_CODE_MORE = 0, DIALOG_CLOSE_CODE_ENOUGH = 1;
	public static final String BUNDLE_KEY_FIELD = "field";
	private ImageView imgMute, imgSave;
	private boolean isMuted;
	private Field mFifteenPiecesView;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//show content on screen
		setContentView(R.layout.game_layout);

		//handle widgets
		imgMute = (ImageView) findViewById(R.id.imgMute);
		imgSave = (ImageView) findViewById(R.id.imgSave);
		isMuted = false;
	    mFifteenPiecesView = (Field) findViewById(R.id.field);
	    mFifteenPiecesView.setGameActivity(this);
		imgMute.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isMuted = !isMuted;
				if (isMuted) {
					imgMute.setImageResource(R.drawable.volumered);
				} else {
					imgMute.setImageResource(R.drawable.volumegreen);
				}
			}
		});
		
 		
		imgSave.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {save();}
		});
		
		Bundle b = getIntent().getExtras();
		
		if(b != null){
			if(b.getBoolean(MenuActivity.KEY_GAME_LOADED, false)) mFifteenPiecesView.load(b);
				else {mFifteenPiecesView.createPieces(); mFifteenPiecesView.shuffle();}; 
		} else {
			mFifteenPiecesView.createPieces();
			mFifteenPiecesView.shuffle();
		}
		
		//if(b != null) {mFifteenPiecesView.load(b);}
	}

	private void save() {
		Intent i = new Intent(this, SaveLoadActivity.class);
		Bundle dump = new Bundle();
		ArrayList<ContentValues> dumpList = mFifteenPiecesView.dump(); 
		dump.putParcelableArrayList(BUNDLE_KEY_FIELD, dumpList);
		dump.putInt(Saver.KEY_GAMES_COMPLETE, mFifteenPiecesView.getCompletePct());
		i.putExtras(dump);
		startActivityForResult(i, MenuActivity.ACTIVITY_SAVELOAD);		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id){
		case DIALOG_GAME_OVER_ID:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("You`ve done it again!");
			builder.setPositiveButton("More", new DialogInterface.OnClickListener() {
			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					onCloseDialog(DIALOG_CLOSE_CODE_MORE);				
				}
			});

			builder.setNegativeButton("Enough", new DialogInterface.OnClickListener() {
			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					onCloseDialog(DIALOG_CLOSE_CODE_ENOUGH);				
				}
			});
			
			dialog = builder.create();
		}
		return dialog;		
	}
	
	private void onCloseDialog(int closeCode){
		switch (closeCode) {
		case DIALOG_CLOSE_CODE_MORE:
			dismissDialog(DIALOG_GAME_OVER_ID);
			mFifteenPiecesView.shuffle();
			break;
		case DIALOG_CLOSE_CODE_ENOUGH:
			setResult(Activity.RESULT_FIRST_USER);
			dismissDialog(DIALOG_GAME_OVER_ID);
			finish();
			break;
		}
		
	}



}
